#include "list.h"

//对比算法库中的sort和list中的sort的效率
void test_op()
{
	srand(time(0));
	const int N = 1000000;
	vector<int> v;
	v.reserve(N);
	list<int> lt1;
	list<int> lt2;

	for (int i = 0; i < N; ++i)
	{
		auto e = rand();
		lt2.push_back(e);
		lt1.push_back(e);
	}

	// 拷贝到vector排序，排完以后再拷贝回来
	int begin1 = clock();
	// 先拷贝到vector
	for (auto e : lt1)
	{
		v.push_back(e);
	}

	// 排序
	sort(v.begin(), v.end());

	// 拷贝回去
	size_t i = 0;
	for (auto& e : lt1)
	{
		e = v[i++];
	}

	int end1 = clock();

	int begin2 = clock();
	lt2.sort();
	int end2 = clock();

	printf("vector sort:%d\n", end1 - begin1);
	printf("list sort:%d\n", end2 - begin2);
}


template<class T>
void print(const cyq::list<T>& lt)
{
	typename cyq::list<T>::const_iterator it = lt.begin();
	while (it != lt.end())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl;

	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;
}

void test_list1()
{
	cyq::list<int> lt;
	lt.push_back(1);
	lt.push_back(2);
	lt.push_back(3);
	lt.push_back(4);
	lt.push_back(5);
	lt.push_back(6);

	cyq::list<int>::iterator it = lt.begin();
	while (it != lt.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;

	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;

}

void test_list2()
{
	struct A
	{
		A(int a1 = 0, int a2 = 0)
			:_a1(a1)
			,_a2(a2)
		{}
		int _a1;
		int _a2;
	};

	cyq::list<A> lt;
	lt.push_back(A(1, 1));
	lt.push_back(A(2, 1));
	lt.push_back(A(3, 1));
	lt.push_back(A(4, 1));
	lt.push_back(A(5, 1));

	cyq::list<A>::iterator it = lt.begin();
	while (it != lt.end())
	{
		cout << (*it)._a1 << " " << (*it)._a2 << endl;
		cout << it->_a1 << " " << it->_a2 << endl;
		it++;
	}

}

void test_list3()
{
	cyq::list<int> lt;
	lt.push_back(1);
	lt.push_back(2);
	lt.push_back(3);
	lt.push_back(4);
	lt.push_back(5);
	lt.push_back(6);
	lt.push_front(0);
	lt.push_front(0);
	lt.push_front(0);
	lt.push_front(0);
	lt.push_front(0);

	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;

	lt.pop_back();
	lt.pop_back();
	lt.pop_front();
	lt.pop_front();

	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;

	lt.clear();
	lt.push_back(10);
	lt.push_back(20);
	lt.push_back(30);
	lt.push_back(40);

	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;

	cout << lt.size() << endl;

}

void test_list4()
{
	cyq::list<int> lt;
	lt.push_back(1);
	lt.push_back(2);
	lt.push_back(3);
	lt.push_back(4);
	lt.push_back(5);
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;

	cyq::list<int> lt1(lt);
	for (auto e : lt1)
	{
		cout << e << " ";
	}
	cout << endl;

	cyq::list<int> lt2;
	lt2.push_back(10);
	lt2.push_back(20);
	lt2.push_back(30);
	lt2.push_back(40);
	lt2.push_back(50);
	for (auto e : lt2)
	{
		cout << e << " ";
	}
	cout << endl;

	lt1 = lt2;
	for (auto e : lt1)
	{
		cout << e << " ";
	}
	cout << endl;
}


int main()
{
	test_list1();
	cout << "-----------------------------------------" << endl;

	test_list2();
	cout << "-----------------------------------------" << endl;

	test_list3();
	cout << "-----------------------------------------" << endl;

	test_list4();
	cout << "-----------------------------------------" << endl;

	cyq::list<int> lt;
	lt.push_back(1);
	lt.push_back(2);
	lt.push_back(3);
	lt.push_back(4);
	lt.push_back(5);
	lt.push_back(6);
	print(lt);
	cout << "-----------------------------------------" << endl;

	test_op();
	cout << "-----------------------------------------" << endl;




	return 0;
}